package com.lzc.springboot.mapper;

import com.lzc.springboot.domain.Farmer;
import com.lzc.springboot.domain.Question;
import org.apache.ibatis.annotations.*;

@Mapper
public interface QuestionMapper {

  /**
   * 添加问题
   * @param question
   */
  @Insert("insert into question values(null,#{fid},#{eid},#{question},#{date},0,null,0)")
  void addQuestion(Question question);

  /**
   * 根据农户id查询未解决问题
   * @param fid
   * @return
   */
  @Select("select * from question where fid=#{fid} and state='0'")
  @Options(useCache = false,flushCache = Options.FlushCachePolicy.TRUE)
  Question[] findAllUnsolved(int fid);

  /**
   * 根据农户id查询已解决问题
   * @param fid
   * @return
   */
  @Select("select * from question where fid=#{fid} and state='1'")
  @Options(useCache = false,flushCache = Options.FlushCachePolicy.TRUE)
  Question[] findAllSolved(int fid);

  /**
   * 根据ID删除问题
   * @param id
   */
  @Delete("delete from question where id=#{id}")
  void delQuestionById(int id);

  /**
   * 根据专家ID查询已经解决的问题
   * @param eid
   * @return
   */
  @Select("select * from question where eid=#{eid} and state='1'")
  @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
  Question[] findAllSolvedByEid(int eid);

  /**
   * 根据专家ID查询未解决的问题
   * @param eid
   * @return
   */
  @Select("select * from question where eid=#{eid} and state='0'")
  @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
  Question[] findAllUnsolvedByEid(int eid);

  /**
   * 问题回复
   * @param question
   */
  @Update("update question set state='1',answer=#{answer} where id=#{id}")
  void answerQuestion(Question question);

  @Select("select q.* from question q,farmer f where q.state='0' and q.fid=f.id and f.name like concat('%',#{name},'%') and f.phone like concat('%',#{phone},'%') and f.email like concat('%',#{email},'%')")
  @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
  Question[] findUnresolvedQuestionByText(Farmer farmer);

  @Select("select q.* from question q,farmer f where q.state='1' and q.fid=f.id and f.name like concat('%',#{name},'%') and f.phone like concat('%',#{phone},'%') and f.email like concat('%',#{email},'%')")
  @Options(useCache = false, flushCache = Options.FlushCachePolicy.TRUE)
  Question[] findSolvedQuestionByText(Farmer farmer);
}
